package com.xana.acg.mikomiko.actis.live.tiktok;

import android.view.View;
import android.widget.TextView;


import com.xana.acg.com.data.DataSource;
import com.xana.acg.com.view.AvatarView;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.fac.model.biliLive.ILiveItem;
import com.xana.acg.fac.model.biliLive.UriResp;
import com.xana.acg.fac.net.NetCallBack;
import com.xana.acg.fac.net.Network;
import com.xana.acg.mikomiko.App;
import com.xana.acg.mikomiko.R;
import com.xana.acg.mikomiko.media.TiktokVideoView;

import butterknife.BindView;
import butterknife.OnClick;

public class TikTokRecyclerViewAdapter extends RecyclerAdapter<ILiveItem> {

    public static final String TAG = "AdapterTikTokRecyclerView";


    public TikTokRecyclerViewAdapter(OnClickListener l) {
        onClickListener = l;
    }

    @Override
    protected int getItemViewType(int position, ILiveItem iLiveItem) {
        return R.layout.item_tiktok;
    }

    @Override
    protected ViewHolder<ILiveItem> onCreateViewHolder(View view, int viewType) {
        return new VH(view);
    }

    static class VH extends RecyclerAdapter.ViewHolder<ILiveItem> implements DataSource.Callback<UriResp> {
        @BindView(R.id.videoplayer)
        TiktokVideoView tikTokView;
        @BindView(R.id.txt_title)
        TextView title;
        @BindView(R.id.txt_nickname)
        TextView name;
        @BindView(R.id.iv_avatar)
        AvatarView avatar;


        String live = "/sdcard/02/a.mp4";

        public VH(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(ILiveItem item) {
            Network.biliAPI().getUrls(item.getRoomid()).enqueue(new NetCallBack<>(this));
            name.setText("@"+item.getUname());
            title.setText(item.getTitle());
            avatar.setSrc(item.getUser_cover());
            tikTokView.setUp(live, "");
        }

        @Override
        public void ok(UriResp data) {
            live = data.getUri();
            tikTokView.setUp(live, "");
        }

        @Override
        public void fail(String msg) {
            App.showToast(msg);
        }

        @OnClick({R.id.moreLiveTextView, R.id.faceTextView})
        void click(View view) {
            if (onClickListener != null) {
                onClickListener.click(view);
            }
        }
    }

    public interface OnClickListener {
        void click(View view);
    }

    public static OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener l) {
        onClickListener = l;
    }
}
